//
//  ScaleAnimateButton.swift
//  douyinSwift
//
//  Created by 于波 on 2019/11/24.
//  Copyright © 2019 于波. All rights reserved.
//

import UIKit

class ScaleAnimateButton: UIButton {
    
    // 当View本身被添加到父视图上时
    // 或者理解为 当view出现在屏幕上的时候
    override func willMove(toWindow newWindow: UIWindow?) {
        super.willMove(toWindow: newWindow)
        
        // 添加一个点击事件
        addTarget(self, action: #selector(toggleSelected), for: .touchUpInside)
    }
    
    @objc func toggleSelected() {
        // toggle 就是true变false   false变true
        isSelected.toggle()
    }
    
    override var isSelected: Bool {
        get {
            super.isSelected
        }
        
        set {
            super.transform = .init(scaleX: 0.8, y: 0.8)
             
            // delay动画延迟  usingSpringWithDamping弹性幅度  initialSpringVelocity弹性初试加速度
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.1, options: [.beginFromCurrentState,.transitionCrossDissolve], animations: {
                super.isSelected = newValue
                super.transform = .identity
            })
        }
    }
}


